package ru.housewife.controllers;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import ru.common.hibernate.HibernateActionExecutor;
import ru.housewife.form.DayMenuForm;
import ru.housewife.form.DayMenuWebWrapperForm;
import ru.housewife.form.UserForm;
import ru.housewife.form.UsersTableForm;
import ru.housewife.model.DayMenu;
import ru.housewife.service.daymenus.AddNewDayMenu;
import ru.housewife.service.daymenus.RemoveDayMenu;

import javax.servlet.http.HttpServletRequest;
import java.util.Calendar;

import static org.springframework.web.bind.annotation.RequestMethod.POST;


/**
 * controller for managing day menus
 *
 * @author Alexander Troshanin
 */
@Controller
public class DayMenuController extends AuthorizedController {

    private final HibernateActionExecutor hiber;

    public DayMenuController(@Qualifier("houseWifesHibernate") HibernateActionExecutor hiber) {
        this.hiber = hiber;
    }

    @RequestMapping(value = "/daymenus-list.html")
    public ModelAndView getDayMenus(long usersTableId, HttpServletRequest request) {
        UserForm user = getUserForm(request);
        ModelAndView ret = new ModelAndView("housewifes/daymenus/dayMenusPage");
        ret.addObject("user", user);
        ret.addObject("userTable", user.getUsersTable(usersTableId));
        return ret;
    }

    @RequestMapping(value = "/daymenus-get.html")
    public Object getDayMenu(long usersTableId, long id, HttpServletRequest request) {
        UserForm user = getUserForm(request);
        UsersTableForm usersTable = user.getUsersTable(usersTableId);
        if (usersTable == null) {
            return "redirect:/userstables.html";
        }
        DayMenuForm dayMenu = usersTable.getDayMenu(id);
        if (dayMenu == null) {
            return "redirect:/userstable-get.html?id=" + usersTable.getId();
        }
        ModelAndView ret = new ModelAndView("housewifes/daymenus/dayMenuCoolPage");
        ret.addObject("usersTable", usersTable);
        ret.addObject("dayMenu", new DayMenuWebWrapperForm(usersTable, dayMenu, dayMenu.getDayNumber(), false, Calendar.getInstance()));
        //ret.addObject("dayMenu", dayMenu);
        return ret;
    }

    @RequestMapping(value = "/daymenus-add.html", method = POST)
    public Object addDayMenu(long usersTableId, DayMenuForm dayMenu, HttpServletRequest request) {
        UserForm user = getUserForm(request);
        UsersTableForm usersTable = user.getUsersTable(usersTableId);
        if (usersTable == null) {
            return "redirect:/userstables.html";
        }
        if (dayMenu.getDayNumber() >= 0 && dayMenu.getDayNumber() < usersTable.getPeriodLength() &&
                usersTable.getDayMenusByDay()[dayMenu.getDayNumber()] == null) {
            dayMenu.setUsersTable(usersTable);
            DayMenu res = hiber.execute(new AddNewDayMenu(dayMenu));
            return "redirect:/daymenus-get.html?usersTableId=" + usersTableId + "&id=" + res.getId();
        } else {
            return "redirect:/userstables.html";
        }
    }

    @RequestMapping(value = "/daymenus-remove.html")
    public Object removeDayMenu(long usersTableId, long id, HttpServletRequest request) {
        UserForm user = getUserForm(request);
        UsersTableForm usersTable = user.getUsersTable(usersTableId);
        if (usersTable == null) {
            return "redirect:/userstables.html";
        }
        DayMenuForm dayMenu = usersTable.getDayMenu(id);
        if (dayMenu == null) {
            return "redirect:/userstable-get.html?id=" + usersTableId;
        }
        hiber.execute(new RemoveDayMenu(dayMenu));
        return "redirect:/daymenus-list.html?usersTableId=" + usersTableId;
    }

}
